<?php

/**
 * @package Framework03
 * @author Walter Tamboer
 */

/**
 * The abstract class for all Html elements.
 */
abstract class FW_Html_Element
{
    /**
     * @var array A list with all the attributes that this element contains.
     */
    protected $attributeList = array();

    /**
     * Adds a new attribute in the attribute list.
     * @param string $name The name of the attribute to add.
     * @param string $value The value of the attribute.
     * @throws FW_Html_Exception
     */
    public function addAttribute($name, $value)
    {
        if (isset($this->AttributeList[$name]))
            throw new FW_Html_Exception('Attribute "'.$name.'" already exists.');

        $this->attributeList[$name] = $value;
    }

    /**
     * Removes an attribute from the attribute list.
     * @param $name the name of the attribute to remove.
     * @throws FW_Html_Exception
     */
    public function removeAttribute($name)
    {
        if (!isset($this->attributeList[$name]))
            throw new FW_Html_Exception('Attribute "'.$name.'" doesn\'t exists.');

        unset($this->attributeList[$name]);
    }

    /**
     * Sets the value of the given attribute name in the attribute list.
     * @param string $Name The name of the attribute to add.
     * @param string $Value The value of the attribute.
     */
    public function setAttribute($name, $value)
    {
        $this->attributeList[$name] = $value;
    }

    /**
     * Gets the attribute with the given name from the attribute list.
     * @param $Name the name of the attribute to get.
     * @return string Returns a string with the value of the requested attribute.
     * @throws FW_Form_Exception
     */
    public function getAttribute($name)
    {
        if (!isset($this->attributeList[$name]))
            throw new FW_HTML_Exception('Attribute "'.$name.'" doesn\'t exists.');

        return $this->attributeList[$name];
    }

    /**
     * Clears all the attributes currently present in the attribute list.
     */
    public function clearAttributes()
    {
        $this->attributeList = array();
    }

    /**
     * Gets the string representation of this element.
     * @return Returns the created HTML text of this element.
     */
    abstract public function render();

    /**
     * Renders all the attributes of this element to a string.
     * @return Returns a HTML string with all the attributes of this element.
     */
    public function renderAttributes()
    {
        $result = '';
        foreach ($this->attributeList as $name => $value)
        {
            $result .= ' '. $name .'="'. $value .'"';
        }
        return $result;
    }
}

?>